/*
 * Copyright (c) 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Aleksey Nikiforov' nor the names of other contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input.mouse;


import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;



/**
 * NOTE. In Linux, if you set "emulate middle button", then
 * the following example won't work when quckly pressing both left
 * and right button:
 * mouseManager.setCommand("myCommand",  new int[]{0, 1});
 * (0 is the left mouse button and 1 is the right mouse button).
 * This happens because the system reports that the middle mouse
 * button is pressed and left and right buttons are not.
 * 
 * @author lex
 *
 */
public class MouseBindingManager {

	@SuppressWarnings("unused")
	private static final Logger log =
		Logger.getLogger(MouseBindingManager.class.getName()); 
	private static MouseBindingManager manager = new MouseBindingManager();

	/**
	 * @return mouseManager singleton
	 */
	public static MouseBindingManager get() {
		return manager;
	}
	
	
	private Map<String, Command> commands = new HashMap<String, Command>();
	
	public MouseBindingManager() { }
	
	public void addCommand(String command, int mouseButton) {
		Command cmd = commands.get(command);
		if (cmd == null) {
			cmd = new Command();
			commands.put(command, cmd);
		}
		
		cmd.addTrigger(new int[]{mouseButton});
	}
	
	public void addCommand(String command, int[] trigger) {
		Command cmd = commands.get(command);
		if (cmd == null) {
			cmd = new Command();
			commands.put(command, cmd);
		}
		
		cmd.addTrigger(trigger);
	}
	
	public void setCommand(String command, int mouseButton) {
		Command cmd = new Command();
		cmd.addTrigger(new int[]{mouseButton});
		commands.put(command, cmd);
	}
	
	public void setCommand(String command, int[] trigger) {
		Command cmd = new Command();
		cmd.addTrigger(trigger);
		commands.put(command, cmd);
	}
	
	public boolean isValidCommand(String command) {
		return isValidCommand(command, true);
	}
	
	public boolean isValidCommand(String command, boolean allowRepeats) {
		Command cmd = commands.get(command);
		if (cmd == null) return false;
		return cmd.isActivated(allowRepeats);
	}
	
}
